/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: MapOverviewFrame.java 35 2008-05-05 14:16:37Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.core.Game;
import albareth.core.GameMap;
import albareth.core.entity.Mobile;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.Tile;
import albareth.frontend.slick.state.InGameState;

public class MapOverviewFrame extends UIElement
{
    public static final Color COLOR_WALL = Color.lightGray;
    public static final Color COLOR_FLOOR = Color.darkGray;
    public static final Color COLOR_PLAYER = Color.yellow;
    public static final Color COLOR_MOBILE = Color.red;
    public static final Color COLOR_ITEM = Color.white;
    
    private static Map _radarColorCache = new HashMap();

    
    private PlayerCharacter _player;
    private int _tileScale;
    
    public MapOverviewFrame(int scale, PlayerCharacter player)
    {
        this(0, 0, 0, 0, scale, player);
    }

    public MapOverviewFrame(int px, int py, int width, int height, int scale, PlayerCharacter player)
    {
        super(px, py, width, height);
        _player = player;
        _tileScale = scale;
        setId(InGameState.COMP_OVERVIEW);
    }

    public void render(Graphics g, GameContainer container, int screenX, int screenY)
            throws SlickException
    {
        GameMap map = _player.getMap();
        int left = (_componentWidth - map.getWidth() * _tileScale) / 2;
        int top = (_componentHeight - map.getHeight() * _tileScale) / 2;
        for (int x = 0; x < map.getWidth(); x++)
        {
            for (int y = 0; y < map.getHeight(); y++)
            {
                Color tileColor;
                Mobile mobile = map.getMobile(x, y);
                List items = map.getItems(x, y);
                
                //select tile color:
                if (!Game.CHEAT_MODE && !map.isExplored(x, y)) {
                    tileColor = Color.black;
                } else if (Game.CHEAT_MODE && mobile != null) {
                    if (mobile.isPlayerControlled()) {
                        tileColor = COLOR_PLAYER;
                    } else {
                        tileColor = COLOR_MOBILE;
                    }
                    
                } else if (Game.CHEAT_MODE && items != null) {
                    tileColor = COLOR_ITEM;
                    
                } else {
                    Tile groundTile = map.getGround(x, y).getTile();
                    tileColor = (Color)_radarColorCache.get(groundTile);
                    if (tileColor == null)
                    {
                        String colString = groundTile.getRadarColor();
                        if (colString != null) {
                            tileColor = Color.decode(colString);
                        } else {
                            tileColor = Color.cyan;
                        }
                        _radarColorCache.put(groundTile, tileColor);
                    }
                }
                
                g.setColor(tileColor);
                g.fillRect(
                        screenX + left + x * _tileScale,
                        screenY + top + y * _tileScale,
                        _tileScale + 1, _tileScale + 1
                );
            }

        }

    }

}
